package com.ee.chat.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ee.chat.admin.service.IComplaintService;
import com.ee.chat.admin.service.IOperationService;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.UserComplaintLogMapper;
import com.ee.chat.common.entity.UserComplaintLog;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author AppiJ
 * @Date 2020/4/7 17:08
 */
@Service
public class ComplaintServiceImpl implements IComplaintService {

    @Resource
    UserComplaintLogMapper complaintLogMapper;
    @Resource
    IOperationService operationService;

    /**
     * 获取投诉列表
     * @Author AppiJ
     * @Date 2020/4/7 17:46
     * @param
     * @return
     */
    @Override
    public Result list(HttpServletRequest request, Integer userId, Integer toUserId, String time,
                       Integer status, Integer page, Integer limit) throws Exception {
        Map<String, Object> data = new HashMap<>();

        if(StringUtil.isEmpty(time) && userId == null && toUserId == null && status == null){
            HttpSession session = request.getSession(true);
            List<String> userPermissions = (List<String>)session.getAttribute("userPermissions");
            if(!userPermissions.contains("/complaint/viewList")){
                data.put("complaints", new ArrayList<>());
                data.put("total", 0);

                return Result.returnSuccess(data);
            }
        }

        Date beginTime = null, endTime = null;
        if(!StringUtil.isEmpty(time)){
            String[] arr = time.split(" - ");
            beginTime = DateUtil.parse(arr[0]);
            endTime = DateUtil.parse(arr[1]);
        }

        Page<Map<String, Object>> p = new Page<>(page, limit);
        IPage<Map<String, Object>> iPage = complaintLogMapper.list(p, userId, toUserId, status, beginTime, endTime);
        List<Map<String, Object>> complaints = iPage.getRecords();
        for(Map<String, Object> comp : complaints){
            //图片
            List<String> images = new ArrayList<>();
            if(!StringUtil.isEmpty(comp.get("images"))){
                String is = comp.get("images")+"";
                is = is.replaceAll("\\\\", "");
                images = JSONObject.parseArray(is, String.class);
            }
            comp.put("images", images);
        }

        data.put("complaints", complaints);
        data.put("total", iPage.getTotal());

        operationService.addOperationLog(request, "查询投诉列表数据");

        return Result.returnSuccess(data);
    }

    /**
     * 处理投诉
     * @Author AppiJ
     * @Date 2020/4/8 15:22
     * @param
     * @return
     */
    @Override
    public Result updateStatus(HttpServletRequest request, List<Integer> ids) throws Exception {

        for(Integer id : ids) {
            UserComplaintLog complaintLog = complaintLogMapper.selectById(id);
            complaintLog.setStatus(1);
            complaintLogMapper.updateById(complaintLog);
        }
        operationService.addOperationLog(request, "处理投诉，id：" + ids);

        return Result.returnSuccess(null);
    }
}
